package com.snow.campuslink.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.Channel;
import com.snow.campuslink.project.constant.Constant.RabbitConstant;
import com.snow.campuslink.user.entity.Email;
import com.snow.campuslink.user.mapper.EmailMapper;
import com.snow.campuslink.user.service.IEmailService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gaosl
 * @since 2021-03-15
 */
@Service
public class EmailServiceImpl extends ServiceImpl<EmailMapper, Email> implements IEmailService {

    @Resource
    JavaMailSenderImpl mailSender;

    @Value("${spring.mail.username}")
    private String sender;

    @RabbitListener(
            bindings=@QueueBinding(
                    value=@Queue(value=RabbitConstant.SEND_VERIFICATION_CODE_EMAIL_USER_EMAIL_QUEUE,autoDelete="false"),
                    exchange=@Exchange(value=RabbitConstant.SEND_VERIFICATION_CODE_EMAIL_USER_LOGIN_EXCHANGE,type= ExchangeTypes.DIRECT),
                    key=""
            )
    )
    public void  process(Channel channel, Message msg) throws MessagingException {
        HashMap<String, String> map = JSON.parseObject(new String(msg.getBody()), HashMap.class);

        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(sender);
        helper.setTo(new String[]{map.get("email")});
        helper.setSubject("邮箱验证码");
        helper.setText(map.get("verificationCode"));
        mailSender.send(mimeMessage);
    }

  /*  public void sendEmail(String subject, String content, String[] to, String[] cc, String[] bcc,
                             String[] files) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            *//** true代表多组件，如附件、图片等 *//*
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setFrom(sender);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content);
            *//** 抄送 *//*
            if (cc != null && cc.length > 0) {
                helper.setCc(cc);
            }
            *//** 密送 *//*
            if (bcc != null && bcc.length > 0) {
                helper.setBcc(bcc);
            }
            if (files!=null&&files.length>0) {
                System.out.println("对附件做处理");
            }
            mailSender.send(mimeMessage);

        } catch (Exception e) {
            e.printStackTrace();
        }
        Email email = new Email(null,subject, content, JSON.toJSONString(to), cc==null?null:JSON.toJSONString(cc), bcc==null?null:JSON.toJSONString(bcc), files==null?null:JSON.toJSONString(files),null,null,null);
        save(email);
    }*/

}

